package com.njlime.core.sdk.wechat;


import com.njlime.cms.service.sys.MaterialNewsService;
import com.njlime.cms.service.sys.MaterialVoiceService;
import com.njlime.core.helper.SpringContextHolder;
import com.njlime.member.service.app.AppMemberService;
import com.njlime.sys.service.app.AppWxConfigService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;


/**
 * 响应来自微信服务器的Token Url验证请求
 * 
 */
public class ReceiveServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
    private AppWxConfigService appWxConfigService;
    private MaterialVoiceService materialVoiceService;
    private MaterialNewsService materialNewsService;

    public AppMemberService appMemberService;
    public ReceiveServlet() {
        super();
        this.appWxConfigService = SpringContextHolder.getBean("appWxConfigService");
        this.appMemberService = SpringContextHolder.getBean("appMemberService");
        this.materialVoiceService = SpringContextHolder.getBean("materialVoiceService");
        this.materialNewsService = SpringContextHolder.getBean("materialNewsService");
    }

	public void destroy() {
		super.destroy(); 
	}

	/**
	 * 验证微信Token
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("TokenServlet doGet");
		 // 微信加密签名  
        String signature = request.getParameter("signature");  
        // 时间戳  
        String timestamp = request.getParameter("timestamp");  
        // 随机数  
        String nonce = request.getParameter("nonce");  
        // 随机字符串  
        String echostr = request.getParameter("echostr");  
        
        String result = "";
        CoreService.processRequest2(request,appWxConfigService);
        if (echostr != null && echostr.length() > 1) {  
            result = echostr;  
        } else {  
            //正常的微信处理流程  
        	System.out.println("收到非验证流程消息");
        }  
  
        try {  
            OutputStream os = response.getOutputStream();  
            os.write(result.getBytes("UTF-8"));  
            os.flush();  
            os.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
        } 

	}

	/**  
     * 处理微信服务器发来的消息  
     */ 
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	    System.out.println("===========回复");
	    try {
            // 将请求、响应的编码均设置为UTF-8（防止中文乱码）
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
            // 调用核心业务类接收消息、处理消息
            String respMessage = CoreService.processRequest(request, appWxConfigService,appMemberService,materialVoiceService,materialNewsService);

            // 响应消息
            PrintWriter out = response.getWriter();
            out.print(respMessage);
            out.close();
        }catch(Exception e){
	        e.printStackTrace();
        }
	}

	public void init() throws ServletException {
		
	}
	
}
